﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using ProjManagSolution.pmMyWebService;

namespace ProjManagSolution
{
    public partial class EditPhase : ChildWindow
    {
        protected Phase m_editPhase = new Phase();
        protected List<milestone> m_ml = new List<milestone>();

        public EditPhase(Phase editPhase, List<milestone> ml)
        {
            m_editPhase = editPhase;
            m_ml = ml;
            InitializeComponent();

            Milestone oM = new Milestone();
            oM.ID = editPhase.MilestoneID;
            oM.Name = editPhase.Milestone;
            
            txtboxPhaseName.Text = editPhase.Name;
            txtboxDescription.Text = editPhase.Description;
            autoboxMilestone.SelectedItem = oM;
            datepickEndDate.SelectedDate = editPhase.EndDate;
            datepickStartDate.SelectedDate = editPhase.StartDate;

            List<Milestone> t = new List<Milestone>();
            for (int i = 0; i < ml.Count; ++i)
            {
                t.Add(new Milestone() { 
                    ID = m_ml[i].milestone_id.ToString(),
                    Description = m_ml[i].milestone_description,
                    EndDate = m_ml[i].milestone_end_date,
                    Name = m_ml[i].milestone_name,
                    StartDate = m_ml[i].milestone_start_date
                });
            }

            //bind to autobox
            autoboxMilestone.ItemsSource = t;
        }

        void ws_GetPhasesCompleted(object sender, GetPhasesCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }


        protected bool Input_Validate()
        {
            if (txtboxPhaseName.Text == "")
            {
                MessageBox.Show("Phase name cannot be empty!");
                return false;
            }
            if ((datepickStartDate.SelectedDate < DateTime.Now) || 
                (datepickEndDate.SelectedDate < DateTime.Now))
            {
                MessageBox.Show("Start date/End date cannot be ealier than today!");
                return false;
            }
            if (DateTime.Compare((DateTime)datepickEndDate.SelectedDate, (DateTime)datepickStartDate.SelectedDate) < 0)
            {
                MessageBox.Show("Start date must be ealier than end date!");
                return false;
            }

            return true;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

